package wlt.plan.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.LocalDate;
import java.util.Objects;

public class TodoItem {
    // 重要等级
    public enum Priority {
        LOW,    // 次要
        MEDIUM, // 中等
        HIGH    // 重要
    }
    
    private final String text;
    private final LocalDate date;
    private boolean completed;
    private final Priority priority;
    
    // 添加默认构造函数，用于Jackson反序列化
    public TodoItem() {
        this.text = "";
        this.date = LocalDate.now();
        this.completed = false;
        this.priority = Priority.MEDIUM;    // 默认中等优先级
    }
    
    // 带参数的构造函数
    public TodoItem(String text, LocalDate date) {
        this(text, date, Priority.MEDIUM);  // 默认中等优先级
    }
    
    public TodoItem(String text, LocalDate date, Priority priority) {
        this.text = text;
        this.date = date;
        this.completed = false;
        this.priority = priority;
    }
    
    // 带Jackson注解的构造函数，明确指定属性映射
    @JsonCreator
    public TodoItem(@JsonProperty ("text") String text,
                    @JsonProperty("date") LocalDate date,
                    @JsonProperty("completed") boolean completed,
                    @JsonProperty("priority") Priority priority) {
        this.text = text;
        this.date = date;
        this.completed = completed;
        this.priority = priority;
    }
    
    public String getText() {
        return text;
    }
    
    public LocalDate getDate() {
        return date;
    }
    
    public boolean isCompleted() {
        return completed;
    }
    
    public void setCompleted(boolean completed) {
        this.completed = completed;
    }
    
    public Priority getPriority() {
        return priority;
    }
    
    // 用于在ListView中显示的文本格式，添加优先级标识
    @Override
    public String toString() {
        String priorityStr = switch (priority) {
            case HIGH   ->  "[重要]";
            case MEDIUM ->  "[中等]";
            case LOW    ->  "[次要]";
        };
        
        return priorityStr + text;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TodoItem todoItem = (TodoItem) o;
        return completed == todoItem.completed &&
               Objects.equals(text, todoItem.text) &&
               Objects.equals(date, todoItem.date) &&
               priority == todoItem.priority;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(text, date, completed, priority);
    }
}
